package com.dlq.cloud.controller;

import com.dlq.cloud.pojo.User;
import com.dlq.cloud.service.impl.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping(value="/user/{userName}")
    @HystrixCommand(fallbackMethod = "hystrixGetUserByName")
    public User getUserByName(@PathVariable(value="userName") String userName){
        User user = userService.getUserByName(userName);
        System.out.println(user);
        user.getLoginName();
        if(user==null){
            System.out.println("1");
            throw new RuntimeException("没有数据");
        }
        return user;
    }

    public User hystrixGetUserByName(@PathVariable(value="userName") String userName){
        System.out.println("2");
        return new User().setId(userName+"用户不存在，请确认用户名").setLoginName(userName);
                //.setPassword(userName+"用户不存在，请确认用户名").setRealName(userName+"用户不存在，请确认用户名")
                //.setSuperman(userName+"用户不存在，请确认用户名");
    }
}
